package feedback

import (
	"gitcode.com/G-YT/ANN/server/global"
	"gitcode.com/G-YT/ANN/server/model/common/request"
	"gitcode.com/G-YT/ANN/server/model/feedback"
	feedbackReq "gitcode.com/G-YT/ANN/server/model/feedback/request"
	"gitcode.com/G-YT/ANN/server/model/public"
	"gitcode.com/G-YT/ANN/server/model/user"
	"gorm.io/gorm"
	"time"
)

type FeedbackService struct {
}

// CreateFeedback 创建Feedback记录
// Author [piexlmax](https://github.com/piexlmax)
func (ffService *FeedbackService) CreateFeedback(ff *feedback.Feedback) (err error) {
	err = global.GVA_DB.Create(ff).Error
	return err
}

// DeleteFeedback 删除Feedback记录
// Author [piexlmax](https://github.com/piexlmax)
func (ffService *FeedbackService) DeleteFeedback(ff feedback.Feedback) (err error) {
	err = global.GVA_DB.Delete(&ff).Error
	return err
}

// DeleteFeedbackByIds 批量删除Feedback记录
// Author [piexlmax](https://github.com/piexlmax)
func (ffService *FeedbackService) DeleteFeedbackByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]feedback.Feedback{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateFeedback 更新Feedback记录
// Author [piexlmax](https://github.com/piexlmax)
func (ffService *FeedbackService) UpdateFeedback(ff feedback.Feedback) (err error) {
	status := true
	ff.Status = &status
	err = global.GVA_DB.Save(&ff).Error
	return err
}

// GetFeedback 根据id获取Feedback记录
// Author [piexlmax](https://github.com/piexlmax)
func (ffService *FeedbackService) GetFeedback(id uint) (ff feedback.Feedback, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&ff).Error
	return
}

// GetFeedbackByUid 根据用户id获取Feedback记录
// Author [g-qs](https://github.com/g-qs)
func (ffService *FeedbackService) GetFeedbackByUid(uid uint) (ff feedback.Feedback, err error) {
	err = global.GVA_DB.Where("u_id = ?", uid).First(&ff).Error
	return
}

// GetFeedbackInfoList 分页获取Feedback记录
// Author [piexlmax](https://github.com/piexlmax)
func (ffService *FeedbackService) GetFeedbackInfoList(info feedbackReq.FeedbackSearch) (list []feedback.Feedback, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&feedback.Feedback{})
	var ffs []feedback.Feedback
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	if info.Status != nil {
		db = db.Where("status = ?", info.Status)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}
	err = db.Preload("User").Find(&ffs).Error
	return ffs, total, err
}

// GetFeedbackInfoListByUid 获取建议信箱列表
// Author [g-qs](https://github.com/g-qs)
func (ffService *FeedbackService) GetFeedbackInfoListByUid(uid uint) (list []feedback.Feedback, err error) {
	var ffs []feedback.Feedback
	err = global.GVA_DB.Where("u_id = ?", uid).Find(&ffs).Error
	return ffs, err
}

// AdoptFeedback 采纳建议
// Author [g-qs](https://github.com/g-qs)
func (ffService *FeedbackService) AdoptFeedback(ID, UID uint) (err error) {
	return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		// 修改采纳状态
		if err = tx.Model(&feedback.Feedback{}).Where("id = ?", ID).Update("adopt_status", true).Error; err != nil {
			return err
		}
		pointsRecord := public.Points{
			Uid:       UID,
			Types:     7,
			CreatedAt: time.Now(),
			Points:    10,
		}
		if prErr := tx.Create(&pointsRecord).Error; prErr != nil {
			return prErr
		}
		if upErr := tx.Model(&user.User{}).Where("ID = ?", UID).Update("points", gorm.Expr("points + ?", 10)).Error; upErr != nil {
			return upErr
		}
		return nil
	})
}
